package com.domor.xxx.system.controller.system;

import com.domor.xxx.common.annotation.Log;
import com.domor.xxx.common.core.controller.BaseController;
import com.domor.xxx.common.core.domain.AjaxResult;
import com.domor.xxx.common.core.page.TableDataInfo;
import com.domor.xxx.framework.shiro.util.ShiroUtils;
import com.domor.xxx.system.domain.SysDictData;
import com.domor.xxx.system.domain.SysDictType;
import com.domor.xxx.system.service.SysDictDataService;
import com.domor.xxx.system.service.SysDictTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <h3>数据字典类别 Controller</h3>
 * @author liyuyang
 * @date 2020/5/4
 **/
@Controller
@RequestMapping("/system/dict")
public class SysDictController extends BaseController {

    private String prefix = "system/dict";

    @Autowired
    private SysDictTypeService dictTypeService;

    @Autowired
    private SysDictDataService dictDataService;


    @GetMapping()
    public String dictType() {
        return prefix + "/index";
    }

    /**
     * 字典类别列表
     */
    @PostMapping("/list")
    @RequiresPermissions("system:dict:list")
    @ResponseBody
    public TableDataInfo list(SysDictType dictType) {
        setOrderByAndPage();
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        return getDataTable(list);
    }

    /**
     * 新增字典类型
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存字典类型
     */
    @Log(title = "字典类型")
    @RequiresPermissions("system:dict:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysDictType dict) {
        if (dictTypeService.checkDictTypeUnique(dict)) {
            return error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
    @GetMapping("/edit/{dictId}")
    public String edit(@PathVariable("dictId") int dictId, ModelMap mmap) {
        mmap.put("dict", dictTypeService.selectDictTypeById(dictId));
        return prefix + "/edit";
    }

    /**
     * 修改保存字典类型
     */
    @Log(title = "字典类型")
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysDictType dict) {
        if (dictTypeService.checkDictTypeUnique(dict)) {
            return error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(dictTypeService.updateDictType(dict));
    }

    @Log(title = "字典类型")
    @RequiresPermissions("system:dict:delete")
    @PostMapping("/delete/{dictId}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("dictId") Integer dictId) {
        return toAjax(dictTypeService.deleteDictTypeById(dictId));
    }

    /****************************字典数据相关接口**********************************/
    /**
     * 字典数据列表
     */
    @PostMapping("/dataList")
    @RequiresPermissions("system:dict:list")
    @ResponseBody
    public TableDataInfo dataList(String dictType) {
        List<SysDictData> list = dictDataService.selectDictDataByType(dictType);
        return getDataTable(list);
    }

    /**
     * 新增字典数据
     */
    @GetMapping("/addData/{dictType}")
    public String addData(@PathVariable("dictType") String dictType, ModelMap mmap) {
        mmap.put("dictType", dictType);
        return prefix + "/addData";
    }

    /**
     * 新增保存字典数据
     */
    @Log(title = "字典数据")
    @RequiresPermissions("system:dict:add")
    @PostMapping("/addData")
    @ResponseBody
    public AjaxResult addDataSave(@Validated SysDictData dict) {
        dict.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改字典数据
     */
    @GetMapping("/editData/{dictId}")
    public String editData(@PathVariable("dictId") int dictId, ModelMap mmap) {
        mmap.put("dict", dictDataService.selectDictDataById(dictId));
        return prefix + "/editData";
    }

    /**
     * 修改保存字典数据
     */
    @Log(title = "字典数据")
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/editData")
    @ResponseBody
    public AjaxResult editDataSave(@Validated SysDictData dict) {
        dict.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除数据字典数据
     */
    @Log(title = "字典数据")
    @RequiresPermissions("system:dict:delete")
    @PostMapping("/deleteData/{dictId}")
    @ResponseBody
    public AjaxResult deleteData(@PathVariable("dictId") int dictId) {
        return toAjax(dictDataService.deleteDictDataById(dictId));
    }

    /**
     * 清空缓存
     */
    @RequiresPermissions("system:dict:delete")
    @Log(title = "字典类型")
    @GetMapping("/clearCache")
    @ResponseBody
    public AjaxResult clearCache() {
        dictTypeService.clearCache();
        return success();
    }

    @ResponseBody
    @PostMapping("/getDictData")
    public List<Map<String, Object>> getDictData(String type) {
        List<SysDictData> data = dictDataService.selectDictDataByType(type);
        List<Map<String, Object>> list = new ArrayList<>();
        for (SysDictData d : data) {
            Map<String, Object> map = new HashMap<>();
            map.put("value", d.getDictValue());
            map.put("text", d.getDictLabel());
            list.add(map);
        }
        return list;
    }

    /**
     * 根据字典类别获取字典项列表(带空白项)
     * 常用于combobox
     * @param type
     * @return
     */
    @ResponseBody
    @PostMapping("/getDictDataWithBlank")
    public List<Map<String, Object>> getDictDataWithBlank(String type) {
        List<Map<String, Object>> list = getDictData(type);
        Map<String, Object> blank = new HashMap<>();
        blank.put("value", -1);
        blank.put("text", "全部");
        blank.put("selected", true);
        list.add(0, blank);
        return list;
    }

}
